//
//  PhotoBrowsePopupView.m
//  ExtModule
//
//  Created by CPX on 2018/9/12.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "PhotoBrowsePopupView.h"
#import "UIButton+WebCache.h"

/**
 图片浏览弹出View
 */
@interface PhotoBrowsePopupView ()
@property (weak, nonatomic) IBOutlet UIButton *imageViewBt;
@property (weak, nonatomic) IBOutlet UIButton *shareBt;
@property (weak, nonatomic) IBOutlet UIButton *saveBt;

@end

@implementation PhotoBrowsePopupView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupViews];
    }
    return self;
}

-(void)setupViews{
    self.frame =  [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];

    
    UIView * alertView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] firstObject];
    [self addSubview:alertView];
    alertView.userInteractionEnabled = YES;
    alertView.tag = 100;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onDismissTap:)];
    [alertView addGestureRecognizer:tap];
 
    
    
    
    [alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        
    }];
    UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    self.transform =  CGAffineTransformMakeScale(0.01, 0.01);
    [self.shareBt setTitle:TLString(@"greeting_cards_share") forState:UIControlStateNormal];
    [self.saveBt setTitle:TLString(@"greeting_cards_download") forState:UIControlStateNormal];


}
- (void)setUrl:(NSString *)url{
    _url = url;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.imageViewBt animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    
    [self.imageViewBt sd_setBackgroundImageWithURL:[NSURL URLWithString:url] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        [hud hideAnimated:YES];

        
    }];
}

-(void)show{
    [UIView animateWithDuration:0.2 animations:^{
        
        self.transform = CGAffineTransformIdentity;
    }];
}
- (IBAction)onTap:(UIButton *)sender {
    [self hide];
    if (sender == self.shareBt){
        self.onShareEvent?self.onShareEvent(sender):nil;
    }
    else{
        self.onSaveToLocalEvent?self.onSaveToLocalEvent(sender):nil;
    }
    
}


-(void)hide{
    [UIView animateWithDuration:0.2 animations:^{
        self.transform =  CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)onDismissTap:(UITapGestureRecognizer * )sender{
    if (sender.view.tag == 100){
        [self hide];
    }
}


@end
